#include "pch.hpp"
#include "GameState.hpp"




GameState::GameState(Game* game, Engine::Script::ContextPool::ptr contextPool, Engine::Script::pointer<asIScriptObject> object)
	: m_game(game)
	, m_contextPool(contextPool)
	, m_object(object)
	, m_activatedId(object->GetObjectType()->GetMethodIdByDecl("void activated(Game@ game)"))
	, m_deactivatedId(object->GetObjectType()->GetMethodIdByDecl("void deactivated(Game@ game)"))
	, m_updateId(object->GetObjectType()->GetMethodIdByDecl("void update(Game@ game)"))
{
	verify(m_activatedId >= 0);
	verify(m_deactivatedId >= 0);
	verify(m_updateId >= 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////




void GameState::activated()
{
	auto context = m_contextPool->context();
	context->prepare(m_activatedId);
	context->setObject(m_object.get());
	context->setArgObject(0, m_game);
	context->execute();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void GameState::deactivated()
{
	auto context = m_contextPool->context();
	context->prepare(m_deactivatedId);
	context->setObject(m_object.get());
	context->setArgObject(0, m_game);
	context->execute();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void GameState::update()
{
	auto context = m_contextPool->context();
	context->prepare(m_updateId);
	context->setObject(m_object.get());
	context->setArgObject(0, m_game);
	context->execute();
}
///////////////////////////////////////////////////////////////////////////////////////////////////




void GameState::declareType(Engine::Script::System::ptr engine)
{
	engine->registerInterface("GameState");
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void GameState::registerType(Engine::Script::System::ptr engine)
{
	engine->registerInterfaceMethod("GameState", "void activated(Game@)");
	engine->registerInterfaceMethod("GameState", "void deactivated(Game@)");
	engine->registerInterfaceMethod("GameState", "void update(Game@)");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
